<?php 

class Controller_Front{
	/**
	 * 
	 * Enter description here ...
	 * @var Controller_UrlMapper
	 */
	private $_urlMapper;
	
	private $_actionDir;
	/**
	 * 
	 * Enter description here ...
	 * @var Request
	 */
	private $_request;
	public function __construct(Controller_UrlMapper $urlMapper,$actionDir){
		$this->_request = Request::getInstace();
		$this->_urlMapper = $urlMapper;
		$this->_urlMapper->setRequest($this->_request);
		$this->_actionDir = $actionDir;
	}
	public function run(){
		
	    
		$this->_urlMapper->parseUrl();
		
		
		$action = $this->_getAction();
		
		$action->doGet();
		
		
		
	}
	/**
	 * 
	 * Enter description here ...
	 * @throws BaseAction
	 */
	private function _getAction(){
		
		$package = $this->_urlMapper->getPackage();
		$actionName = $this->_urlMapper->getActionName();
		$filename = $this->_actionDir;
		if($package)
			$filename .= DIRECTORY_SEPARATOR.$package;
		$filename = $filename.DIRECTORY_SEPARATOR.$actionName.'.php';
		
		if(file_exists($filename)){
			require_once $filename;
			 return new $actionName($this->_request);
		}
		
		 
		 $lang = Lang::singleton('error');
		 
		 throw new Exception($lang->get('no_action',$actionName));
		 
		
	}
	
	
	
}